package com.ncmmall.mall.controller.business.distribution;

import com.ncmmall.application.business.distribution.DistributionSettingApplication;
import com.feijin.commons.lang.Requires;
import com.ncmmall.domain.business.distribution.DistributionSetting;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 分销商设置管理
 *
 * @author likaikai
 * @date 2020-8-20 19:57:02
 */
@Controller
@RequestMapping("/distribution/setting")
@Slf4j
public class DistributionSettingController {

    @Autowired
    private DistributionSettingApplication distributionSettingApplication;

    /**
     * 修改合伙人申请
     */
    @RequestMapping(method = RequestMethod.GET, value = "/applyNote")
    public String modifyApplyNote(Model model) {
        DistributionSetting setting = distributionSettingApplication.findLastSetting();
        model.addAttribute("applyNote", setting == null ? "" : setting.getApplyNote());
        return "mall/distribution/setting/applyNote";
    }

    /**
     * 修改合伙人申请
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/applyNote")
    @ResponseBody
    public Map<String, Object> modifyApplyNote(String note) {
        try {
            Requires.hasLength(note, "内容不能为空");
            distributionSettingApplication.modifyApplyNote(note, Securitys.currentUser());
            return ImmutableMap.of("status", true, "message", "修改成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[修改合伙人申请信息异常]", e);
            return ImmutableMap.of("status", false, "message", "修改异常");
        }
    }

    /**
     * 分销设置
     *
     * @param flag 1、分销设置
     *             2、结算设置
     */
    @RequestMapping(value = "/modify", method = RequestMethod.GET)
    public String modify(Model model, @RequestParam(defaultValue = "1") int flag) {
        DistributionSetting setting = distributionSettingApplication.findLastSetting();
        if (setting == null) {
            setting = new DistributionSetting();
        }
        model.addAttribute("setting", setting);
        model.addAttribute("flag", flag);

        if (flag == 1) {
            return "mall/distribution/setting/form";
        } else {
            return "mall/distribution/setting/form2";
        }
    }

    /**
     * 分销设置
     *
     * @param flag 1、分销设置
     *             2、结算设置
     */
    @RequestMapping(value = "/modify", method = {RequestMethod.PUT, RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> modify(int flag, DistributionSetting params) {
        try {
            distributionSettingApplication.modify(flag, params, Securitys.currentUser());
            return ImmutableMap.of("status", true, "message", "修改成功");
        } catch (IllegalArgumentException e) {
            return ImmutableMap.of("status", false, "message", e.getMessage());
        } catch (Exception e) {
            log.error("[修改分销设置异常]", e);
            return ImmutableMap.of("status", false, "message", "修改异常");
        }
    }

}
